package com.hevs.samplewebapp.server.dao;

import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.appengine.api.datastore.Key;
import com.hevs.samplewebapp.server.entities.MedicalRecord;
import com.hevs.samplewebapp.server.entities.User;
/**
 * Implementation of the DAO interface for the Medical Datas
 * 
 * @author Lionel Rey
 * @date 08.03.12
 */
public class MedicalRecordDAOImpl implements MedicalRecordDAO {

	@Override
	/**
	 * Get all the medical datas
	 * @return
	 * list of medical data
	 */
	public List<MedicalRecord> getAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	/**
	 * Create new medical datas
	 * @param medicalRecords
	 * list of medical data to create
	 */
	public void add(List<MedicalRecord> medicalRecords) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
			for(MedicalRecord medicalRecord : medicalRecords){
				medicalRecord.setTimestamp(new Date());
				pm.makePersistent(medicalRecord);
			}
		}
		finally{
			pm.close();
		}
	}

	@Override
	/**
	 * Update a medicale data or create a new one
	 * @param medicalRecord
	 * the medical record to update
	 */
	public void update(MedicalRecord medicalRecord) {
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
			if(medicalRecord.getKey()!=null){
				double value = medicalRecord.getValue();
				int category = medicalRecord.getCategory();
				int type = medicalRecord.getType();
				Date date = medicalRecord.getDate();
				medicalRecord = pm.getObjectById(MedicalRecord.class, medicalRecord.getKey());
				medicalRecord.setValue(value);
				medicalRecord.setCategory(category);
				medicalRecord.setType(type);
				medicalRecord.setDate(date);
				pm.makePersistent(medicalRecord);
			}
			else{
				
				medicalRecord.setTimestamp(new Date());
				pm.makePersistent(medicalRecord);
			}	
		}
		finally{
			pm.close();
		}	
	}

	@Override
	/**
	 * Remove a medical data
	 * @param medicalRecord
	 * the medical data to delete
	 */
	public void remove(MedicalRecord medicalRecord) {
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
			pm.deletePersistent(medicalRecord);
		}
		finally{
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	/**
	 * Methode to retrieve all medical records for a specific user
	 * @param user
	 * owner of the medical data
	 * @return
	 * the list of medical data
	 */
	public List<MedicalRecord> getAll(User user) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		//pm.getFetchPlan().setGroup(FetchGroup.ALL);

		Query query = pm.newQuery(MedicalRecord.class);
		
	    query.setFilter("user == userParam");
	    query.declareImports("import com.google.appengine.api.datastore.Key;");
	    query.declareParameters("Key userParam");
	    try{
	    	List<MedicalRecord> results = (List<MedicalRecord>) query.execute(user.getKey());
	    	return (List<MedicalRecord>) pm.detachCopyAll(results);
	    }
		finally{
			pm.close();
		}
	}
	
	@Override
	/**
	 * Method to retrieve 1 medical data by its key
	 * @param key
	 * medical record Key
	 * @return
	 * a medical record
	 */
	public MedicalRecord getMedicalRecord(Key key) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		try{
			MedicalRecord medicalRecord = pm.getObjectById(MedicalRecord.class, key);
			return pm.detachCopy(medicalRecord);
		}
		finally{
			pm.close();
		}
		
	}
}
